<template>
  <div id="theme">
    <BaseDialog :title="$t('安装模板')" :dialog-show.sync="dialogShow" width="50%" class="dialog">
      <el-form ref="themeForm" :model="themeForm" :rules="themeRules" label-position="top">
        <el-form-item :label="`${$t('模板')}:`" prop="theme">
          <el-input v-model="themeForm.theme" disabled />
        </el-form-item>
        <el-form-item :label="`${$t('站点')}:`" prop="website_id">
          <el-select v-model="themeForm.website_id" :placeholder="$t('请选择站点')" style="width: 100%" @change="handleChange">
            <el-option v-for="item in options" :key="item.id" :label="item.domain" :value="item.id" />
          </el-select>
        </el-form-item>
        <div class="btn">
          <el-button @click="dialogShow = false">{{ $t('取消') }}</el-button>
          <el-button type="primary" @click="submittheme()">
            {{ $t('提交') }}
          </el-button>
        </div>
      </el-form>
    </BaseDialog>
    <el-radio-group v-model="params.lang" size="medium" @change="changeLang">
      <el-radio-button v-for="item in langList" :key="item.id" :label="item.lang">
        {{ item.lang }}
      </el-radio-button>
    </el-radio-group>
    <BaseTable :request="request" :params.sync="params" ref="baseTable" :is-request="false">
      <el-table-column :label="$t('模板')" prop="theme" />
      <el-table-column :label="$t('模板名称')" prop="name" />
      <el-table-column :label="$t('作者')" prop="author" />
      <el-table-column :label="$t('语言')" prop="lang" />
      <el-table-column :label="$t('模板描述')" prop="description" />
      <el-table-column :label="$t('操作')" width="150px" fixed="right" align="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="$t('更新')" placement="top">
            <el-button type="text" icon="el-icon-refresh" @click="update(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" :content="$t('卸载')" placement="top">
            <el-button type="text" icon="el-icon-delete" @click="uninstall(scope.row)"></el-button>
          </el-tooltip>
          <BaseDropdown @command="handleCommand($event, scope.row)">
            <template>
              <el-dropdown-item command="all-files">
                {{ $t('所有文件') }}
              </el-dropdown-item>
              <el-dropdown-item command="theme">
                {{ $t('模版文件') }}
              </el-dropdown-item>
              <el-dropdown-item command="enable" :disabled="scope.row.is_active == 1 ? 'disabled' : false">
                {{ $t('启用') }}
              </el-dropdown-item>
              <el-dropdown-item command="design">
                {{ $t('可视化设计') }}
              </el-dropdown-item>
            </template>
          </BaseDropdown>
        </template>
      </el-table-column>
    </BaseTable>
  </div>
</template>
<script>
import mouldApi from '@/api/theme.js'
import websettingApi from '@/api/websetting'
export default {
  data() {
    return {
      request: mouldApi.installList,
      params: {
        lang: '',
        website_id: ''
      },
      themeList: [],
      dialogShow: false,
      options: [],
      themeForm: {
        website_id: ''
      },
      themeRules: {
        website_id: [
          { required: 'true', message: this.$t('请选择站点'), trigger: 'blur' }
        ]
      },
      searchForm: {},
      siteId: '',
      langList: [],
      lang: ''
    }
  },
  computed: {
    siteId1() {
      return this.$store.getters.siteId
    }
  },
  watch: {
    siteId1: {
      handler(val) {
        if (val) {
          this.siteId = val
          this.params.website_id = val
          this.getLang()
        }
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    this.siteId = this.$store.getters.siteId
    this.params.website_id = this.$store.getters.siteId
    // this.themeForm.website_id = this.$store.getters.siteId
    if (this.siteId) {
      this.getLang()
    }
  },
  methods: {
    // 更多操作
    handleCommand(key, row) {
      switch (key) {
        case 'all-files':
          this.allFiles(row)
          break
        case 'theme':
          this.temFiles(row)
          break
        case 'enable':
          this.enable(row)
          break
        case 'design':
          this.viewDesign(row)
          break
      }
    },
    viewDesign() {
      const url =
        '/design/index?website_id=' + this.siteId + '&lang=' + this.params.lang
      window.open(url)
    },
    // 获取语言
    getLang() {
      websettingApi.langInfo({ site_id: this.siteId }).then(res => {
        this.langList = res.data
        if (res.data.length > 0) {
          this.params.lang = res.data[0].lang
        }
        this.$refs.baseTable.getList('search')
      })
    },
    // 切换语言
    changeLang(val) {
      this.params.lang = val
      this.$refs.baseTable.getList('search')
    },
    // 启用
    enable(row) {
      this.$confirm(this.$t('是否启用该模板'), this.$t('提示'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(() => {
          const val = {
            theme_id: row.id,
            website_id: this.siteId,
            lang: row.lang
          }
          mouldApi.themeActive(val).then(res => {
            if (res.code == 0) {
              this.$message.success(res.msg)
              this.$refs.baseTable.getList()
            }
          })
        })
        .catch(() => { })
    },
    // 更新
    update(row) {
      this.$confirm(this.$t('是否更新该模板'), this.$t('提示'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(() => {
          const val = {
            theme_id: row.id,
            website_id: this.siteId,
            theme: row.theme
          }
          mouldApi.themeUpdate(val).then(res => {
            if (res.code == 0) {
              this.$message.success(res.msg)
              this.$refs.baseTable.getList()
            }
          })
        })
        .catch(() => { })
    },
    // 卸载
    uninstall(row) {
      this.$confirm(this.$t('是否卸载该模板'), this.$t('提示'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(() => {
          const val = {
            theme_id: row.id,
            website_id: this.siteId
          }
          mouldApi.themeUninstall(val).then(res => {
            if (res.code == 0) {
              this.$message.success(res.msg)
              this.$refs.baseTable.getList()
            }
          })
        })
        .catch(() => { })
    },
    // 所有文件
    allFiles(row) {
      console.log(row, '====')
      this.$router.push({
        name: 'files',
        query: { id: row.website_id, lang: row.lang }
      })
    },
    // 模板文件
    temFiles(row) {
      this.$router.push({
        name: 'tempFiles',
        query: {
          id: row.website_id,
          tid: row.id,
          tname: row.theme,
          lang: row.lang
        }
      })
    },
    submittheme() {
      mouldApi.saveTheme({ ...this.themeForm }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.dialogShow = false
        }
      })
    },
    // // 获取站点列表
    // getWeblist() {
    //   websiteApi.websiteList().then(res => {
    //     this.options = res.data
    //     this.searchForm.website_id = res.data[0].id
    //     this.getList()
    //   })
    // },
    handleChange(val) {
      this.themeForm.website_id = val
    }
  }
}
</script>
<style lang="scss">
#theme {
  .el-radio-group {
    margin-bottom: 16px;

    .el-radio-button {
      margin-right: 13px;
    }

    .el-radio-button__orig-radio:checked+.el-radio-button__inner {
      background-color: #fff7f0;
      border: none;
      color: var(--huocms-com-color-primary);
    }

    .el-radio-button__inner {
      border-radius: 34px;
      border: none;
      font-size: 14px;
      padding: 0;
      width: 96px;
      height: 34px;
      line-height: 34px;
      text-align: center;
      box-shadow: none;
      background-color: #f7f8fa;
    }

    .el-radio-button--medium .el-radio-button__inner {
      width: 96px;
      height: 34px;
      line-height: 34px;
    }
  }

  .el-form-item__content {
    width: 320px;
  }
}
</style>
<style lang="scss" scoped>
.el-button ::v-deep {

  .el-icon-refresh,
  .el-icon-delete {
    font-weight: bold;
  }
}
</style>
